package xyz.labmem.base.sys.util

import org.springframework.data.redis.connection.Message
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener
import org.springframework.data.redis.listener.RedisMessageListenerContainer
import org.springframework.stereotype.Component
import xyz.labmem.base.core.consts.log

/**
 * @Author lty
 * @Date 2021/12/14 15:48
 */
@Component
class RedisKeyListener(listenerContainer: RedisMessageListenerContainer) : KeyExpirationEventMessageListener(
    listenerContainer
) {

    private val log = this.log()

    /**
     * redis key销毁时回调，进行数据处理
     */
    override fun onMessage(message: Message, pattern: ByteArray?) {
        // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
        val expiredKey = message.toString()
        log.info("RedisKeyListener key[$expiredKey] 失效")
    }

}